<?php
//资金明细
namespace app\adminbackstage\model;

use think\facade\Db;
use think\Model;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Detailed extends Model{
    // 模型初始化
    protected static function init()
    {
        parent::init();
        //TODO:初始化内容
    }
    
    public function listDetailed($page=1,$num=10,$where){
        $list = $this::name('detailed')
        ->field('d.uid,d.amount_money,d.fee,d.state,d.type,d.balance,d.remarks,d.a_id as pro_num,d.no_order,d.addtime,d.to_id,u.username,u.phone,u.type as u_type')
        ->alias('d')
        ->leftJoin('user u','u.uid = d.uid')
        ->where($where)
        ->order('id','desc')
        ->limit(($page-1)*$num,$num)
        ->select();
        return $list;
    }
    
    public function listAllDetailed($page=1,$num=10){
        $list = $this::name('detailed')
        ->order('id','desc')
        ->limit(($page-1)*$num,$num)
        ->select();
        return $list;
    }
    
    public function countDetailed($where) {
        $count = $this::name('detailed')
        ->alias('d')
        ->leftJoin('user u','u.uid = d.uid')
        ->where($where)
        ->count();
        return $count;
    }
    
    public function countAllDetailed() {
        $count = $this::name('detailed')
        ->order('id','desc')
        ->count();
        return $count;
    }
    
    //资金明细添加
    public function addDetailed($data){
        $in = $this::name('detailed')->insertGetId($data);
        return $in;
    }
    
    public function listRoyalty($page=1,$num=10,$where){
        $list = $this::name('detailed')
        ->where($where)
        ->order('id','desc')
        ->limit(($page-1)*$num,$num)
        ->select();
        return $list;
    }
    
    public function countRoyalty($where){
        $count = $this::name('detailed')
        ->where($where)
        ->count();
        return $count;
    }

    //获取商家邀请奖励发放列表
    public function merchantExtensionRewardsDetailed($where,$page,$num){
        return Db::name('detailed')
            ->alias('d')
            ->field('d.id,d.amount_money,d.remarks,d.addtime,u1.phone as u1phone,u2.phone as u2phone')
            ->join('user u1','u1.uid = d.uid')
            ->join('user u2','u2.uid = d.to_id')
            ->where($where)
            ->order('d.id desc')
            ->page($page,$num)
            ->select();
    }

    //获取商家邀请家里发放总条数
    public function merchantExtensionRewardsCount($where){
        return Db::name('detailed')
            ->alias('d')
            ->join('user u1','u1.uid = d.uid')
            ->join('user u2','u2.uid = d.to_id')
            ->where($where)
            ->count();
    }

    //渠道商月结账单 列表
    public function channelMonthCommissionList($where,$page,$num){
        return Db::name('detailed')
            ->alias('d')
            ->field('d.id,d.uid,d.amount_money,d.remarks,d.addtime,uc1.channel_name,uc1.p_id,uc2.spread,u.phone')
            ->join('user_channel uc1','uc1.uid = d.uid')
            ->join('user_channel uc2','uc2.id = uc1.p_id')
            ->join('user u','u.uid = d.uid')
            ->where($where)
            ->order('d.id desc')
            ->page($page,$num)
            ->select();
    }

    //渠道商月结账单 总数据数
    public function channelMonthCommissionCount($where){
        return Db::name('detailed')
            ->alias('d')
            ->join('user_channel uc1','uc1.uid = d.uid')
            ->join('user_channel uc2','uc2.id = uc1.p_id')
            ->join('user u','u.uid = d.uid')
            ->where($where)
            ->count();
    }
    
        
    public static function dSql($sql){
        return self::query($sql);
    }
}
